package com.wbdgj.ui.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.adapter.LGRecycleViewAdapter;
import com.wbdgj.adapter.LGViewHolder;
import com.wbdgj.adapter.NewsAdapter;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.base.BaseFragment;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.ui.news.NewsDetailActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.SpacesItemDecoration;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.refresh.PullListView;
import com.wbdgj.utils.refresh.PullToRefreshLayout;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * Created by Administrator on 2018/5/2.
 */

public class MessageFragment extends BaseFragment{

    private Context context;

    private String TYPE_ID;
    @BindView(R.id.search_list)
    RecyclerView search_list;
    private MyAdapter myAdapter;

    @BindView(R.id.editTxt)
    EditText editTxt;

    private ArrayList<LinkedTreeMap> msgCommonList;

    @Override
    protected int getLayout() {
        return R.layout.ui_message_fragment;
    }

    @Override
    protected void onInit() {
        context = getActivity();
        setGridLayout(search_list, context, 3);
        int space = 20;
        search_list.addItemDecoration(new SpacesItemDecoration(space));
    }

    @OnClick({})
    public void onClick(View view) {
        switch (view.getId()) {


        }
    }



    @Override
    public void fetchData() {
    }

    public void showMessageFromActivity(String TYPE_ID){
        this.TYPE_ID = TYPE_ID;
        queryByTypeID(TYPE_ID);
    }


    public void Commit(String branchid){
        addMsg(branchid);
    }

    private void queryByTypeID(String str) {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().queryByTypeID(str);
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    msgCommonList = new ArrayList<>();
                    msgCommonList = (ArrayList<LinkedTreeMap>) linkedTreeMap.get("msgCommonList");
                    myAdapter = new MyAdapter(msgCommonList);
                    search_list.setAdapter(myAdapter);

                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

    private void addMsg(String BRANCH_ID) {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().addMsg(BRANCH_ID, editTxt.getText().toString(), TYPE_ID, "", BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    ToastUtils.toastShort("提交成功");
                    editTxt.setText("");
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

    public class MyAdapter extends LGRecycleViewAdapter<LinkedTreeMap> {

        public MyAdapter(ArrayList<LinkedTreeMap> dataList) {
            super(dataList);
        }

        @Override
        public int getLayoutId(int viewType) {
            return R.layout.item_search_list;
        }

        @Override
        public void convert(LGViewHolder holder, final LinkedTreeMap s, int position) {
            TextView textView = (TextView) holder.getView(R.id.text_view);
            textView.setText(s.get("NAME")+"");
            holder.getView(R.id.type_linear).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    editTxt.setText(editTxt.getText().toString()+s.get("NAME")+"");
                }
            });
        }
    }

    public void setGridLayout(RecyclerView pullRecycleView, Context mContext, int spanCount) {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, spanCount);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        pullRecycleView.setLayoutManager(gridLayoutManager);
    }



}
